<?php

    /**
     * Abstract
     *
     * @package      Core
     * @subpackage   Mail
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    abstract class Core_Mail_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
    //                          Attributes                                    //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Template default extension
         */
        const TEMPLATE_EXT = '.tpl';

        /**
         * Instance parameters
         *
         * @var     array
         */
        protected $_params = array();

        /**
         * Templates end directory
         *
         * @var     string
         */
        protected $_end = 'frontend';

        /**
         * Registry instance
         *
         * @var     Zend_Registry
         */
        protected $_registry = array();

        /**
         * Template
         *
         * @var     array
         */
        protected $_templates = array();

    ////////////////////////////////////////////////////////////////////////////
    //                          Public Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       string      $end
         */
        public function __construct($end = 'frontend')
        {
            $this->_registry    = Zend_Registry::getInstance();
            $this->_end         = $end;
        }

        /**
         * Gets a parameter value
         *
         * @param       string      $param : parameter name
         * @param       string      $defaultValue : the default value returned if the parameter doesn't exists
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      string      the value
         */
        public function getParam($param, $defaultValue = null)
        {
            if ($param === null) {
                throw new Core_ArgumentNullException('param is a null reference');
            }

            if (strlen(trim($param)) == 0) {
                throw new Core_ArgumentException('param is a zero-length string');
            }

            return array_key_exists($param, $this->_params) ? $this->_params[$param] : $defaultValue;
        }

        /**
         * Gets all parameters
         *
         * @return       array
         */
        public function getParams()
        {
            return $this->_params;
        }

        /**
         * Adds or changes the value of a parameter
         *
         * @param       string      $param : parameter name
         * @param       string      $value : parameter value
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      Core_Url_Abstract
         */
        public function setParam($param, $value)
        {
            if ($param === null) {
                throw new Core_ArgumentNullException('param is a null reference');
            }

            if (strlen(trim($param)) == 0) {
                throw new Core_ArgumentException('param is a zero-length string');
            }

            $this->_params[$param] = $value;
            return $this;
        }

        /**
         * Deletes a parameter
         *
         * @param       string      $param : parameter name
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      void
         */
        public function deleteParam($param)
        {
            if ($param === null) {
                throw new Core_ArgumentNullException('param is a null reference');
            }

            if (strlen(trim($param)) == 0) {
                throw new Core_ArgumentException('param is a zero-length string');
            }

            if (array_key_exists($param, $this->_params)) {
                unset($this->_params[$param]);
            }
        }

        /**
         * Clears all parameters
         *
         * @return       void
         */
        public function clearParam()
        {
            $this->_params = array();
        }

        /**
         * Sets template
         *
         * @param       string      $template
         * @return      void
         */
        public function setTemplate($template)
        {
            $fileHtml = $this->_getTemplatesPath($template, 'html');
            $fileText = $this->_getTemplatesPath($template, 'text');

            $templateHtml = new Dwoo_Template_File($fileHtml);
            $templateText = new Dwoo_Template_File($fileText);

            $this->_templates = array(
                                      'html' => $templateHtml,
                                      'text' => $templateText
                                     );
        }

        /**
         * Gets template
         *
         * @return       string
         */
        public function getTemplate($type = 'html')
        {
            return $this->_templates[$type];
        }

    ////////////////////////////////////////////////////////////////////////////
    //                          Protected Methods                             //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Gets template path
         *
         * @param       string      $type
         * @return      string
         */
        protected function _getTemplatesPath($template, $type = 'html')
        {
            $path = Core_IO_Path::combine(
                                            PROJECT_WWW_PATH,
                                            'skin',
                                            $this->_end,
                                            'templates',
                                            'emails',
                                            $type,
                                            $template  . self::TEMPLATE_EXT
                                         );

            if (!Core_IO_File::exists($path)) {
                $path = Core_IO_Path::combine(
                                SERUM_WWW_PATH,
                                'skin',
                                $this->_end,
                                'templates',
                                'emails',
                                $type,
                                $template  . self::TEMPLATE_EXT
                             );

            }

            return $path;
        }
    }
